```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java线程可见性问题解决方案</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background-color: #f8fafc;
            color: #1e293b;
        }
        .hero-bg {
            background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
        }
        .code-block {
            background-color: #1e293b;
            border-left: 4px solid #7c3aed;
        }
        .solution-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .highlight-text {
            background: linear-gradient(90deg, #7c3aed 0%, #4f46e5 100%);
            -webkit-background-clip: text;
            background-clip: text;
            color: transparent;
        }
    </style>
</head>
<body class="antialiased">
    <!-- Hero Section -->
    <section class="hero-bg text-white">
        <div class="container mx-auto px-6 py-24">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-4xl md:text-5xl font-bold mb-6 font-serif">Java线程<span class="highlight-text">可见性</span>问题</h1>
                <p class="text-xl md:text-2xl mb-8 leading-relaxed opacity-90">
                    多线程编程中的关键挑战与高效解决方案
                </p>
                <div class="w-24 h-1 bg-purple-300 mx-auto mb-12"></div>
                <p class="text-lg md:text-xl max-w-2xl mx-auto leading-relaxed">
                    当一个线程修改了共享变量的值时，其他线程可能无法立即看到这个修改。了解如何确保线程间共享变量的修改能够被及时可见。
                </p>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-16 max-w-6xl">
        <!-- Introduction -->
        <section class="mb-20">
            <div class="bg-white rounded-xl shadow-lg p-8">
                <h2 class="text-2xl font-bold mb-6 text-purple-800 flex items-center">
                    <i class="fas fa-lightbulb mr-3"></i> 核心概念
                </h2>
                <p class="text-gray-700 leading-relaxed mb-6">
                    线程可见性问题是多线程编程中的一个关键问题，它指的是当一个线程修改了共享变量的值时，其他线程可能无法立即看到这个修改。
                </p>
                <p class="text-gray-700 leading-relaxed mb-6">
                    Java内存模型(JMM)定义了线程如何与内存交互，以及线程间的通信机制。为了解决线程可见性问题，Java提供了几种解决方案和机制，确保线程之间对共享变量的修改能够被及时可见。
                </p>
                
                <!-- Mermaid Diagram -->
                <div class="mt-10 mb-8">
                    <div class="mermaid">
                        graph TD
                            A[线程A 修改共享变量] -->|写入主内存| B(主内存)
                            B -->|读取| C[线程B]
                            C -->|可能不及时| D[线程可见性问题]
                            D --> E[解决方案]
                            E --> F[volatile关键字]
                            E --> G[synchronized关键字]
                            E --> H[final关键字]
                            E --> I[ThreadLocal]
                            E --> J[原子变量]
                    </div>
                </div>
            </div>
        </section>

        <!-- Solutions Grid -->
        <section class="grid grid-cols-1 md:grid-cols-2 gap-8 mb-20">
            <!-- Solution 1 -->
            <div class="solution-card bg-white rounded-xl shadow-lg overflow-hidden transition-all duration-300 ease-in-out">
                <div class="p-6">
                    <div class="flex items-center mb-4">
                        <div class="bg-purple-100 text-purple-800 p-3 rounded-full mr-4">
                            <i class="fas fa-bolt text-xl"></i>
                        </div>
                        <h3 class="text-xl font-bold text-gray-800">volatile关键字</h3>
                    </div>
                    <p class="text-gray-600 mb-4">确保对该字段的写操作对所有线程立即可见。</p>
                    <ul class="space-y-2 mb-6 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-purple-500 mt-1 mr-2"></i>
                            <span>每次读写直接从主内存中操作</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-purple-500 mt-1 mr-2"></i>
                            <span>写操作会插入内存屏障</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-purple-500 mt-1 mr-2"></i>
                            <span>适用于标志位、状态变量等场景</span>
                        </li>
                    </ul>
                    <div class="code-block rounded-lg p-4 mb-4">
                        <pre class="text-purple-200 overflow-x-auto"><code>public class SharedResource {
    private volatile boolean flag = false;

    public void setFlagTrue() {
        flag = true;
    }

    public boolean isFlagTrue() {
        return flag;
    }
}</code></pre>
                    </div>
                </div>
            </div>

            <!-- Solution 2 -->
            <div class="solution-card bg-white rounded-xl shadow-lg overflow-hidden transition-all duration-300 ease-in-out">
                <div class="p-6">
                    <div class="flex items-center mb-4">
                        <div class="bg-purple-100 text-purple-800 p-3 rounded-full mr-4">
                            <i class="fas fa-lock text-xl"></i>
                        </div>
                        <h3 class="text-xl font-bold text-gray-800">synchronized关键字</h3>
                    </div>
                    <p class="text-gray-600 mb-4">确保同一时间只有一个线程可以执行代码块，并保证可见性。</p>
                    <ul class="space-y-2 mb-6 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-purple-500 mt-1 mr-2"></i>
                            <span>使用内存屏障保证可见性</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-purple-500 mt-1 mr-2"></i>
                            <span>保证线程互斥访问共享资源</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-purple-500 mt-1 mr-2"></i>
                            <span>适用于临界区、共享资源读写</span>
                        </li>
                    </ul>
                    <div class="code-block rounded-lg p-4 mb-4">
                        <pre class="text-purple-200 overflow-x-auto"><code>public class Counter {
    private int count = 0;

    public synchronized void increment() {
        count++;
    }

    public synchronized int getCount() {
        return count;
    }
}</code></pre>
                    </div>
                </div>
            </div>

            <!-- Solution 3 -->
            <div class="solution-card bg-white rounded-xl shadow-lg overflow-hidden transition-all duration-300 ease-in-out">
                <div class="p-6">
                    <div class="flex items-center mb-4">
                        <div class="bg-purple-100 text-purple-800 p-3 rounded-full mr-4">
                            <i class="fas fa-anchor text-xl"></i>
                        </div>
                        <h3 class="text-xl font-bold text-gray-800">final关键字</h3>
                    </div>
                    <p class="text-gray-600 mb-4">保证在对象构造完成后，final变量的值对所有线程可见。</p>
                    <ul class="space-y-2 mb-6 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-purple-500 mt-1 mr-2"></i>
                            <span>构造函数中的赋值对所有线程可见</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-purple-500 mt-1 mr-2"></i>
                            <span>JMM保证final变量初始化的可见性</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-purple-500 mt-1 mr-2"></i>
                            <span>适用于单例模式中的常量</span>
                        </li>
                    </ul>
                    <div class="code-block rounded-lg p-4 mb-4">
                        <pre class="text-purple-200 overflow-x-auto"><code>public class Singleton {
    private static final Singleton instance = new Singleton();

    private Singleton() {}

    public static Singleton getInstance() {
        return instance;
    }
}</code></pre>
                    </div>
                </div>
            </div>

            <!-- Solution 4 -->
            <div class="solution-card bg-white rounded-xl shadow-lg overflow-hidden transition-all duration-300 ease-in-out">
                <div class="p-6">
                    <div class="flex items-center mb-4">
                        <div class="bg-purple-100 text-purple-800 p-3 rounded-full mr-4">
                            <i class="fas fa-user-shield text-xl"></i>
                        </div>
                        <h3 class="text-xl font-bold text-gray-800">ThreadLocal</h3>
                    </div>
                    <p class="text-gray-600 mb-4">为每个线程提供独立的变量副本，避免线程干扰。</p>
                    <ul class="space-y-2 mb-6 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-purple-500 mt-1 mr-2"></i>
                            <span>每个线程访问自己的副本</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-purple-500 mt-1 mr-2"></i>
                            <span>完全避免线程间干扰</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-purple-500 mt-1 mr-2"></i>
                            <span>适用于数据库连接、用户会话等</span>
                        </li>
                    </ul>
                    <div class="code-block rounded-lg p-4 mb-4">
                        <pre class="text-purple-200 overflow-x-auto"><code>public class ThreadLocalExample {
    private static final ThreadLocal&lt;Integer&gt; threadLocal = 
        ThreadLocal.withInitial(() -> 0);

    public void setValue(int value) {
        threadLocal.set(value);
    }

    public int getValue() {
        return threadLocal.get();
    }
}</code></pre>
                    </div>
                </div>
            </div>

            <!-- Solution 5 -->
            <div class="solution-card bg-white rounded-xl shadow-lg overflow-hidden transition-all duration-300 ease-in-out md:col-span-2">
                <div class="p-6">
                    <div class="flex items-center mb-4">
                        <div class="bg-purple-100 text-purple-800 p-3 rounded-full mr-4">
                            <i class="fas fa-atom text-xl"></i>
                        </div>
                        <h3 class="text-xl font-bold text-gray-800">原子变量类 (java.util.concurrent.atomic)</h3>
                    </div>
                    <p class="text-gray-600 mb-4">使用CAS机制保证操作的原子性和可见性。</p>
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                        <div>
                            <ul class="space-y-2 mb-6 text-gray-700">
                                <li class="flex items-start">
                                    <i class="fas fa-check-circle text-purple-500 mt-1 mr-2"></i>
                                    <span>使用底层CAS机制保证原子性</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-check-circle text-purple-500 mt-1 mr-2"></i>
                                    <span>保证修改对所有线程可见</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-check-circle text-purple-500 mt-1 mr-2"></i>
                                    <span>适用于计数器、标志位等场景</span>
                                </li>
                            </ul>
                        </div>
                        <div class="code-block rounded-lg p-4">
                            <pre class="text-purple-200 overflow-x-auto"><code>import java.util.concurrent.atomic.AtomicInteger;

public class AtomicCounter {
    private final AtomicInteger count = new AtomicInteger(0);

    public void increment() {
        count.incrementAndGet();
    }

    public int getCount() {
        return count.get();
    }
}</code></pre>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Comparison Table -->
        <section class="bg-white rounded-xl shadow-lg p-8 mb-20">
            <h2 class="text-2xl font-bold mb-6 text-purple-800 flex items-center">
                <i class="fas fa-balance-scale mr-3"></i> 解决方案比较
            </h2>
            <div class="overflow-x-auto">
                <table class="min-w-full divide-y divide-gray-200">
                    <thead class="bg-gray-50">
                        <tr>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">解决方案</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">可见性保证</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">原子性保证</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">性能</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">适用场景</th>
                        </tr>
                    </thead>
                    <tbody class="bg-white divide-y divide-gray-200">
                        <tr class="hover:bg-gray-50">
                            <td class="px-6 py-4 whitespace-nowrap font-medium text-gray-900">volatile</td>
                            <td class="px-6 py-4 whitespace-nowrap text-gray-500">强</td>
                            <td class="px-6 py-4 whitespace-nowrap text-gray-500">无</td>
                            <td class="px-6 py-4 whitespace-nowrap text-gray-500">高</td>
                            <td class="px-6 py-4 text-gray-500">标志位、状态变量</td>
                        </tr>
                        <tr class="hover:bg-gray-50">
                            <td class="px-6 py-4 whitespace-nowrap font-medium text-gray-900">synchronized</td>
                            <td class="px-6 py-4 whitespace-nowrap text-gray-500">强</td>
                            <td class="px-6 py-4 whitespace-nowrap text-gray-500">强</td>
                            <td class="px-6 py-4 whitespace-nowrap text-gray-500">低</td>
                            <td class="px-6 py-4 text-gray-500">临界区、共享资源</td>
                        </tr>
                        <tr class="hover:bg-gray-50">
                            <td class="px-6 py-4 whitespace-nowrap font-medium text-gray-900">final</td>
                            <td class="px-6 py-4 whitespace-nowrap text-gray-500">初始化可见</td>
                            <td class="px-6 py-4 whitespace-nowrap text-gray-500">无</td>
                            <td class="px-6 py-4 whitespace-nowrap text-gray-500">最高</td>
                            <td class="px-6 py-4 text-gray-500">不变对象</td>
                        </tr>
                        <tr class="hover:bg-gray-50">
                            <td class="px-6 py-4 whitespace-nowrap font-medium text-gray-900">ThreadLocal</td>
                            <td class="px-6 py-4 whitespace-nowrap text-gray-500">不适用</td>
                            <td class="px-6 py-4 whitespace-nowrap text-gray-500">不适用</td>
                            <td class="px-6 py-4 whitespace-nowrap text-gray-500">高</td>
                            <td class="px-6 py-4 text-gray-500">线程私有数据</td>
                        </tr>
                        <tr class="hover:bg-gray-50">
                            <td class="px-6 py-4 whitespace-nowrap font-medium text-gray-900">原子变量</td>
                            <td class="px-6 py-4 whitespace-nowrap text-gray-500">强</td>
                            <td class="px-6 py-4 whitespace-nowrap text-gray-500">强</td>
                            <td class="px-6 py-4 whitespace-nowrap text-gray-500">中高</td>
                            <td class="px-6 py-4 text-gray-500">计数器、标志位</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </section>

        <!-- Best Practices -->
        <section class="bg-white rounded-xl shadow-lg p-8">
            <h2 class="text-2xl font-bold mb-6 text-purple-800 flex items-center">
                <i class="fas fa-star mr-3"></i> 最佳实践
            </h2>
            <div class="grid grid-cols-1 md:grid-cols-2 gap-8">
                <div>
                    <h3 class="text-lg font-semibold mb-4 text-gray-800 flex items-center">
                        <i class="fas fa-check-circle text-purple-500 mr-2"></i> 如何选择合适的方案
                    </h3>
                    <ul class="space-y-3 text-gray-700">
                        <li class="flex items-start">
                            <span class="bg-purple-100 text-purple-800 rounded-full w-6 h-6 flex items-center justify-center mr-3 flex-shrink-0">1</span>
                            <span>优先考虑使用final变量，如果适用</span>
                        </li>
                        <li class="flex items-start">
                            <span class="bg-purple-100 text-purple-800 rounded-full w-6 h-6 flex items-center justify-center mr-3 flex-shrink-0">2</span>
                            <span>对于简单的状态标志，优先使用volatile</span>
                        </li>
                        <li class="flex items-start">
                            <span class="bg-purple-100 text-purple-800 rounded-full w-6 h-6 flex items-center justify-center mr-3 flex-shrink-0">3</span>
                            <span>需要原子操作时，使用原子变量类</span>
                        </li>
                        <li class="flex items-start">
                            <span class="bg-purple-100 text-purple-800 rounded-full w-6 h-6 flex items-center justify-center mr-3 flex-shrink-0">4</span>
                            <span>复杂的同步需求，使用synchronized</span>
                        </li>
                        <li class="flex items-start">
                            <span class="bg-purple-100 text-purple-800 rounded-full w-6 h-6 flex items-center justify-center mr-3 flex-shrink-0">5</span>
                            <span>线程特定的数据使用ThreadLocal</span>
                        </li>
                    </ul>
                </div>
                <div>
                    <h3 class="text-lg font-semibold mb-4 text-gray-800 flex items-center">
                        <i class="fas fa-exclamation-triangle text-purple-500 mr-2"></i> 常见误区
                    </h3>
                    <ul class="space-y-3 text-gray-700">
                        <li class="flex items-start">
                            <span class="bg-red-100 text-red-800 rounded-full w-6 h-6 flex items-center justify-center mr-3 flex-shrink-0">!</span>
                            <span>认为volatile能保证原子性（只能保证可见性）</span>
                        </li>
                        <li class="flex items-start">
                            <span class="bg-red-100 text-red-800 rounded-full w-6 h-6 flex items-center justify-center mr-3 flex-shrink-0">!</span>
                            <span>过度使用synchronized导致性能问题</span>
                        </li>
                        <li class="flex items-start">
                            <span class="bg-red-100 text-red-800 rounded-full w-6 h-6 flex items-center justify-center mr-3 flex-shrink-0">!</span>
                            <span>忽视final关键字的可见性保证</span>
                        </li>
                        <li class="flex items-start">
                            <span class="bg-red-100 text-red-800 rounded-full w-6 h-6 flex items-center justify-center mr-3 flex-shrink-0">!</span>
                            <span>ThreadLocal使用后未清理导致内存泄漏</span>
                        </li>
                        <li class="flex items-start">
                            <span class="bg-red-100 text-red-800 rounded-full w-6 h-6 flex items-center justify-center mr-3 flex-shrink-0">!</span>
                            <span>依赖默认的可见性假设而非明确的同步机制</span>
                        </li>
                    </ul>
                </div>
            </div>
        </section>
    </div>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>
```